﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex.Xdf {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BitFlex.IO;
    using System.IO;

    /// <summary>
    /// Represents a physical XdfDocument on disk that can be loaded and saved
    /// </summary>
    public class XdfDocument : DataFileBase {
        /// <summary>
        /// Initializes a new instance of the BitFlex.Xdf.XdfDocument class
        /// </summary>
        /// <param name="directory">The directory that the document resides</param>
        /// <param name="name">The name of the file</param>
        public XdfDocument(string directory, string name)
            : base(directory, name) {
        }

        /// <summary>
        /// Initializes a new instance of the BitFlex.Xdf.XdfDocument class
        /// </summary>
        public XdfDocument()
            : base(string.Empty, string.Empty) {
        }

        /// <summary>
        /// Gets the documents innert text
        /// </summary>
        public string InnerText {
            get {
                return GetInnerText(RootElement);
            }
        }

        /// <summary>
        /// Gets or sets the root element of the document
        /// </summary>
        public XdfSection RootElement {
            get;
            set;
        }

        private string GetInnerText(XdfSection RootElement) {
            XdfBuilder builder = new XdfBuilder();
            builder.WriteSection(RootElement);
            return builder.ToString();
        }

        /// <summary>
        /// Loads the Xdf from the specified file
        /// </summary>
        /// <param name="directory">The directory in which the file resides</param>
        /// <param name="name">The name of the file</param>
        public void LoadFrom(string directory, string name) {
            XdfReader reader = new XdfReader();
            RootElement = reader.LoadFrom(directory, name);
        }

        /// <summary>
        /// Loads the Xdf from the specified Xdf string
        /// </summary>
        /// <param name="xdf">The string to load</param>
        public void LoadFrom(string xdf) {
            XdfReader reader = new XdfReader();
            RootElement = reader.LoadFrom(xdf);
        }

        /// <summary>
        /// Loads the XdfDocument
        /// </summary>
        public override void Load() {
            this.LoadFrom(this.Directory, this.Name);
        }

        /// <summary>
        /// Saves the XdfDocument
        /// </summary>
        public override void Save() {
            XdfBuilder builder = new XdfBuilder();
            builder.WriteSection(RootElement);
            File.WriteAllText(FullName, builder.ToString());
        }
    }
}